package dao;

import model.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import utils.JDBCUtils;

import java.sql.SQLException;

public class UserDao {
    public void addUser(User user) throws SQLException {
        QueryRunner r=new QueryRunner(JDBCUtils.getDataSource());
        String sql = "insert into user(username,email,password,name,phone,address,isadmin,isvalidate) values(?,?,?,?,?,?,?,?)";
        r.update(sql,user.getUsername(),user.getEmail(),user.getPassword(),user.getName(),user.getPhone(),user.getAddress(),user.isIsadmin(),user.isIsvalidate());
    }
    public boolean isUsernameExist(String username) throws SQLException {
        QueryRunner r=new QueryRunner(JDBCUtils.getDataSource());
        String sql="select * from user where username=?";
        User u=r.query(sql,new BeanHandler<User>(User.class),username);
        if (u==null){
            return false;
        }else {
            return true;
        }
    }
    public boolean isEmailExist(String email) throws SQLException {
        QueryRunner r=new QueryRunner(JDBCUtils.getDataSource());
        String sql="select * from user where email=?";
        User u=r.query(sql,new BeanHandler<User>(User.class),email);
        if (u==null){
            return false;
        }else {
            return true;
        }
    }
    public User selectByUsernamePassword(String username,String password) throws SQLException {
        QueryRunner r = new QueryRunner(JDBCUtils.getDataSource());
        String sql = "select * from user where username=? and password=?";
        return r.query(sql, new BeanHandler<User>(User.class), username, password);
    }

    public User selectByEmailPassword(String email,String password) throws SQLException {
        QueryRunner r = new QueryRunner(JDBCUtils.getDataSource());
        String sql = "select * from user where email=? and password=?";
        return r.query(sql, new BeanHandler<User>(User.class), email, password);
    }

}
